package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Hotel;
import cn.wolfcode.trip.base.mapper.HotelMapper;
import cn.wolfcode.trip.base.query.HotelQueryObjext;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IHotelService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HotelServiceImpl implements IHotelService {

    @Autowired
    private HotelMapper mapper;

    @Override
    public void save(Hotel hotel) {
        //如果用户没有填写需要就自动生成一个
        if(hotel.getSequence()==null){
            int count = mapper.getMaxSequence(hotel.getRegion().getId());
            ++count;
            hotel.setSequence(count);
        }
        mapper.insert(hotel);
    }

    @Override
    public void update(Hotel hotel) {
        mapper.updateByPrimaryKey(hotel);
    }

    @Override
    public PageInfo query(HotelQueryObjext qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Hotel> list=mapper.queryForList(qo);
        return new PageInfo(list);
    }

    @Override
    public Hotel get(Long id) {
        return mapper.selectByPrimaryKey(id);
    }
}
